﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MyZip;

namespace MyZip
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        

        private void button2_Click(object sender, EventArgs e)
        {
            Stream myStream;
            Engine ffile = new Engine();
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;


            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    string fileName = openFileDialog1.FileName;
                    textBox1.Text = Path.GetFullPath(fileName);

                    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);

                    Decompression engine = new Decompression();
                    byte[] filex = new byte[fs.Length];
                    fs.Read(filex, 0, (int)fs.Length);
                    engine.DecompressionFunc(filex);
                    

                    string fApplicationPath = AppDomain.CurrentDomain.BaseDirectory; // 应用程序所有的路径
                    string fDirectoryPath = Path.Combine(fApplicationPath, "解压缩"); // 文件夹的完整路径
                    string fPath = Path.Combine(fDirectoryPath, Path.GetFileName(fileName)); // 图片的新路径
                    textBox2.Text = fPath;

                    SaveFile(fPath.Substring(0, fPath.Length-4), engine.outResult, engine.outResultLength);

                    myStream.Close();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Stream myStream;
            Engine ffile = new Engine();

            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;


            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    
                    string fileName = openFileDialog1.FileName;
                    textBox1.Text = Path.GetFullPath(fileName);

                    //string filex = ffile.GetZippedFile(contents);//压缩后的文件以字符串形式出现
                    Compression engine = new Compression();
                    string fApplicationPath = AppDomain.CurrentDomain.BaseDirectory; // 应用程序所有的路径
                    string fDirectoryPath = Path.Combine(fApplicationPath, "压缩"); // 文件夹的完整路径
                    string fPath = Path.Combine(fDirectoryPath, Path.GetFileName(fileName))+".YLQ"; // 图片的新路径
                    textBox2.Text = fPath;

                    
                    engine.Input = new byte[myStream.Length];
                    engine.InputLength = myStream.Read(engine.Input, 0, (int)myStream.Length);
                    engine.CompressionFunc();


                    SaveFile(fPath+".cyg", engine.Output, engine.OutputLength);

                    myStream.Close();
                }
            }
        }
        protected void SaveFile(string fileName, byte[] filex, int length)
        {
            try
            {
                Stream stream = File.OpenWrite(fileName);
                {
                    stream.Write(filex, 0, length);
                    stream.Close();
                }
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
